package cn.itcast.fore.web.action;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.bos.service.take_delivery.OrderService;
import cn.itcast.crm.service.Customer;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
@Namespace("/")//<package namespace extends
@ParentPackage("struts-default")
@Results({
	@Result(name="success",type="redirect",location="/order-success.html")
})
public class OrderAction extends ActionSupport implements ModelDriven<Order>{

	private Order model=new Order();
	public Order getModel() {
		return model;
	}
	
	@Autowired
	private OrderService orderProxy;
	
	private String sendAreaInfo;//寄件人信息:省市区
	private String recAreaInfo; //收件人信息:省市区
	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}
	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}


	/**
	  * @Description: 封装订单对象，调用bos完成订单保存
	  * @return
	  * @throws Exception
	  *	  
	 */
	@Action("orderAction_add")
	public String add() throws Exception {
		try {
			//寄件人省市区形式：北京市/北京市/昌平区 实体中需要Area对象
			Area area;
			if(StringUtils.isNotBlank(sendAreaInfo)){
				String[] strings = sendAreaInfo.split("/");
				String province = strings[0];
				String city = strings[1];
				String district = strings[2];
				area = new Area(province, city, district);
				model.setSendArea(area);
			}
			if(StringUtils.isNotBlank(recAreaInfo)){
				String[] strings = recAreaInfo.split("/");
				String province = strings[0];
				String city = strings[1];
				String district = strings[2];
				area = new Area(province, city, district);
				model.setRecArea(area);
			}
			//还可以存客户信息,因为如果是登录状态下单可以存客户信息,到时候给客户积分
			Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("loginCustomer");
			if(customer!=null){
				model.setCustomer_id(customer.getId());
			}
			orderProxy.save(model);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
}







